virt customize

virt-customize tools

ref: https://libguestfs.org/virt-customize.1.html

1
sudo apt install -y libguestfs-tools

command help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
virt-customize --help
virt-customize: customize a virtual machine

virt-customize [--options] -d domname

virt-customize [--options] -a disk.img [-a disk.img ...]

A short summary of the options is given below. For detailed help please
read the man page virt-customize(1).

Options:
-a, --add <file> Add disk image file
--append-line <FILE:LINE> Append line(s) to the file
--attach <iso> Attach data disk/ISO during install
--attach-format <format> Set attach disk format
-c, --connect <uri> Set libvirt URI
--chmod <PERMISSIONS:FILE> Change the permissions of a file
--color, --colors, --colour, --colours
Use ANSI colour sequences even if not tty
--commands-from-file <FILENAME> Read customize commands from file
--copy <SOURCE:DEST> Copy files in disk image
--copy-in <LOCALPATH:REMOTEDIR> Copy local files or directories into image
-d, --domain <domain> Set libvirt guest name
--delete <PATH> Delete a file or directory
-n, --dryrun, --dry-run Perform a dry run
--echo-keys Don’t turn off echo for passphrases
--edit <FILE:EXPR> Edit file using Perl expression
--firstboot <SCRIPT> Run script at first guest boot
--firstboot-command <'CMD+ARGS'> Run command at first guest boot
--firstboot-install <PKG,PKG..> Add package(s) to install at first boot
--format <format> Set format (default: auto)
--help Display brief help
--hostname <HOSTNAME> Set the hostname
--install <PKG,PKG..> Add package(s) to install
--key <SELECTOR> Specify a LUKS key
--keys-from-stdin Read passphrases from stdin
--link <TARGET:LINK[:LINK..]> Create symbolic links
-m, --memsize <mb> Set memory size
--mkdir <DIR> Create a directory
--move <SOURCE:DEST> Move files in disk image
--network Enable appliance network (default)
--no-logfile Scrub build log file
--no-network Disable appliance network
--password <USER:SELECTOR> Set user password
--password-crypto <md5|sha256|sha512>
Set password crypto
-q, --quiet Don’t print progress messages
--root-password <SELECTOR> Set root password
--run <SCRIPT> Run script in disk image
--run-command <'CMD+ARGS'> Run command in disk image
--scrub <FILE> Scrub a file
--selinux-relabel Relabel files with correct SELinux labels
--sm-attach <SELECTOR> Attach to a subscription-manager pool
--sm-credentials <SELECTOR> Credentials for subscription-manager
--sm-register Register using subscription-manager
--sm-remove Remove all the subscriptions
--sm-unregister Unregister using subscription-manager
--smp <vcpus> Set number of vCPUs
--ssh-inject <USER[:SELECTOR]> Inject a public key into the guest
--timezone <TIMEZONE> Set the default timezone
--touch <FILE> Run touch on a file
--truncate <FILE> Truncate a file to zero size
--truncate-recursive <PATH> Recursively truncate all files in directory
--uninstall <PKG,PKG..> Uninstall package(s)
--update Update packages
--upload <FILE:DEST> Upload local file to destination
-V, --version Display version and exit
-v, --verbose Enable libguestfs debugging messages
--write <FILE:CONTENT> Write file
-x Enable tracing of libguestfs calls

1 设置root密码

1
2
virt-customize -a ubuntu-20.04-server-cloudimg-amd64.img \
--root-password password:lab123

output:

1
2
3
4
5
6
7
[   0.0] Examining the guest ...
[ 4.3] Setting a random seed
virt-customize: warning: random seed could not be set for this type of
guest
[ 4.4] Setting the machine ID in /etc/machine-id
[ 4.4] Setting passwords
[ 5.8] Finishing off

2 在Image中安装软件包

1
2
virt-customize -a ubuntu-20.04-server-cloudimg-amd64.img \
--install net-tools,unzip

output:

1
2
3
4
5
6
[   0.0] Examining the guest ...
[ 2.2] Setting a random seed
virt-customize: warning: random seed could not be set for this type of
guest
[ 2.3] Installing packages: net-tools unzip
[ 9.2] Finishing off

3 copy 文件到镜像中

1
2
virt-customize -a ubuntu-20.04-server-cloudimg-amd64.img \
--upload ../http_test:/http_test

output:

1
2
3
4
5
6
[   0.0] Examining the guest ...
[ 2.2] Setting a random seed
virt-customize: warning: random seed could not be set for this type of
guest
[ 2.2] Uploading: ../http_test to /http_test
[ 2.3] Finishing off

4 修改时区

1
2
3
virt-customize -a ubuntu-20.04-server-cloudimg-amd64.img \
--timezone "Asia/Shanghai"

5 上传SSH公钥

1
2
virt-customize -a ubuntu-20.04-server-cloudimg-amd64.img  \
--ssh-inject lab:file:./id_rsa.pub

6 在文件中添加内容

1
2
virt-customize -a ubuntu-20.04-server-cloudimg-amd64.img  \
--append-line '/etc/hosts:10.0.0.1 foo'

7 create a user in image

1
2
3
4
5
6
virt-customize -a ubuntu-20.04-server-cloudimg-amd64.img  \
--run-command 'useradd test -s /bin/bash -m'

virt-customize -a ubuntu-20.04-server-cloudimg-amd64.img \
--password test:password:lab123

output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[   0.0] Examining the guest ...
[ 2.2] Setting a random seed
virt-customize: warning: random seed could not be set for this type of
guest
[ 2.3] Running: useradd test -s /bin/bash -m
[ 2.4] Finishing off


[ 0.0] Examining the guest ...
[ 2.2] Setting a random seed
virt-customize: warning: random seed could not be set for this type of
guest
[ 2.3] Setting passwords
[ 3.6] Finishing off