首页 > 系统 > Ubuntu > 正文

VMware下扩展ubuntu虚拟机根目录分区

2024-06-28 16:04:28
字体:
来源:转载
供稿:网友

原文链接:http://t.cn/RzCP9E2 翻译源链接:http://imcczy.com/how-to-expand-the-root-filesystem-in-vmware.html

转载说明:

此文下面的有重新需要分区的操作,分区的操作与正常装系统分区不同,是需要手动指定扇区(sectors)的起始点的, 这里给出计算方式,其实当打出命令时也是有提示的 类似这样的提示 Disk /dev/sda: 15 GiB, 16106127360 bytes, 31457280 sectors Units: sectors of 1 * 512 = 512 bytes sectors 就是你的硬盘当前分区总扇区大小 可以先计算出自己打算要分出的分区大小 例如 根分区 需要14G swap分1G 1024*14=14336bytes(1.4mb) *1024*1024 = 15032385536bytes(14G) 15032385536 / 512 = 29360128 sectors 得出了从起始点到此扇区结束就是14G了

年代久远,谨慎尝试

虚拟机用着用着空间就不够了,google了一篇文章,试了一下,有用,征得作者同意,翻译了下。删了一些无关的东西。

注:

作者输入命令时都用“sudo bash”,用sudo或者su就可以了。

再注:

涉及到磁盘分区表删除,最好先备份整个虚拟机!

检查文件系统:

cruz@Ubuntu:~$ sudo bash[sudo] passWord for cruz: root@ubuntu:~# df -hFilesystem Size Used Avail Use% Mounted on/dev/sda1 9.0G 2.7G 5.9G 32% /udev 488M 4.0K 488M 1% /devtmpfs 199M 800K 198M 1% /runnone 5.0M 0 5.0M 0% /run/locknone 497M 76K 496M 1% /run/shmroot@ubuntu:~#

检查磁盘分区表:

root@ubuntu:~# fdisk -l /dev/sdaDisk /dev/sda: 10.7 GB, 10737418240 bytes255 heads, 63 sectors/track, 1305 cylinders, total 20971520 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00001decDevice Boot Start End Blocks Id System/dev/sda1 * 2048 18874367 9436160 83 linux/dev/sda2 18876414 20969471 1046529 5 Extended/dev/sda5 18876416 20969471 1046528 82 Linux swap / Solarisroot@ubuntu:~#

记住上面显示的交换分区大小(Blocks的数目),这里就是1046528。如果交换分区和根目录不在一个磁盘(比如/dev/sdb),就不要记了。在本文,交换分区在/dev/sda,需要重新分区。

先关闭linux:

root@ubuntu:~# shutdown -h now

在虚拟机设置,硬盘,实用工具下选择扩展。扩展虚拟机的最大磁盘大小,这里将磁盘设置为15G,然后重启。

重新设置分区表要删除所有的旧分区、关闭系统的swap:

cruz@ubuntu:~$ sudo bash[sudo] password for cruz: root@ubuntu:~# free -m total used free shared buffers cachedMem: 992 924 67 0 43 426 -/+ buffers/cache: 454 537Swap: 1021 0 1021root@ubuntu:~# swapoff -aroot@ubuntu:~# free -m total used free shared buffers cachedMem: 992 924 67 0 43 426 -/+ buffers/cache: 454 537Swap: 0 0 0root@ubuntu:~#

接下来的步骤会删除/dev/sda1和/dev/sda2,一定要记住分区表的起始位置,这里是2048!

root@ubuntu:~# fdisk /dev/sdaCommand (m for help): pDisk /dev/sda: 16.1 GB, 16106127360 bytes255 heads, 63 sectors/track, 1958 cylinders, total 31457280 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00001dec Device Boot Start End Blocks Id System/dev/sda1 * 2048 18874367 9436160 83 Linux/dev/sda2 18876414 20969471 1046529 5 Extended/dev/sda5 18876416 20969471 1046528 82 Linux swap / SolarisCommand (m for help): dPartition number (1-5): 1Command (m for help): dPartition number (1-5): 2Command (m for help): pDisk /dev/sda: 16.1 GB, 16106127360 bytes255 heads, 63 sectors/track, 1958 cylinders, total 31457280 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00001dec Device Boot Start End Blocks Id SystemCommand (m for help):

不要退出fdisk,接着创建新分区

Command (m for help): nPartition type: p PRimary (0 primary, 0 extended, 4 free) e extendedSelect (default p): pPartition number (1-4, default 1): 1First sector (2048-31457279, default 2048): Using default value 2048Last sector, +sectors or +size{K,M,G} (2048-31457279, default 31457279): 30410751Command (m for help): nPartition type: p primary (1 primary, 0 extended, 3 free) e extendedSelect (default p): pPartition number (1-4, default 2): Using default value 2First sector (30410752-31457279, default 30410752): Using default value 30410752Last sector, +sectors or +size{K,M,G} (30410752-31457279, default 31457279): Using default value 31457279注意记得创建交换分区,大小别搞错,这里是1046528(31457279-30410571=1046528),即要留一部分block用于创建swap。修改sda2的分区类型为82,即交换分区。Command (m for help): pDisk /dev/sda: 16.1 GB, 16106127360 bytes255 heads, 63 sectors/track, 1958 cylinders, total 31457280 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00001dec Device Boot Start End Blocks Id System/dev/sda1 2048 30410751 15204352 83 Linux/dev/sda2 30410752 31457279 523264 83 LinuxCommand (m for help): tPartition number (1-4): 2Hex code (type L to list codes): 82Changed system type of partition 2 to 82 (Linux swap / Solaris)Command (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.WARNING: Re-reading the partition table failed with error 16: Device or resource busy.The kernel still uses the old table. The new table will be used atthe next reboot or after you run partprobe(8) or kpartx(8)Syncing disks.root@ubuntu:~#

重启虚拟机:

root@ubuntu:~# shutdown -r now

交换分区挂载需要UUID标识符。创建新的交换分区不会匹配旧的UUID,导致重启无法挂载swap分区。这里主要有两种解决方法:在/etc/fstab里写入新的UUID,或者直接将旧的UUID覆盖在新分区上,这里选择后者。 awk命令用来显示旧的UUID,dd命令确保分区没数据。

cruz@ubuntu:~$ sudo bash[sudo] password for cruz: root@ubuntu:~# awk '/swap/ { print $1 }' /etc/fstab#UUID=8bb62351-4436-47df-92fe-af2865f03461root@ubuntu:~# swapoff -aroot@ubuntu:~# free -m total used free shared buffers cachedMem: 992 695 296 0 23 325-/+ buffers/cache: 346 645Swap: 0 0 0root@ubuntu:~# dd if=/dev/zero of=/dev/sda2dd: writing to '/dev/sda2': No space left on device1046529+0 records in1046528+0 records out535822336 bytes (536 MB) copied, 11.9388 s, 44.9 MB/sroot@ubuntu:~# mkswap -U 8bb62351-4436-47df-92fe-af2865f03461 /dev/sda2Setting up swapspace version 1, size = 523260 KiBno label, UUID=8bb62351-4436-47df-92fe-af2865f03461root@ubuntu:~# swapon -aroot@ubuntu:~# free -m total used free shared buffers cachedMem: 992 693 298 0 23 325-/+ buffers/cache: 345 646Swap: 510 7 503root@ubuntu:~#

最后,调整分区大小:

root@ubuntu:~# df -hFilesystem Size Used Avail Use% Mounted on/dev/sda1 9.0G 2.8G 5.8G 33% /udev 488M 4.0K 488M 1% /devtmpfs 199M 788K 198M 1% /runnone 5.0M 0 5.0M 0% /run/locknone 497M 200K 496M 1% /run/shmroot@ubuntu:~# resize2fs /dev/sda1resize2fs 1.42 (29-Nov-2011)Filesystem at /dev/sda1 is mounted on /; on-line resizing requiredold_desc_blocks = 1, new_desc_blocks = 1Performing an on-line resize of /dev/sda1 to 3801088 (4k) blocks.The filesystem on /dev/sda1 is now 3801088 blocks long.root@ubuntu:~# df -hFilesystem Size Used Avail Use% Mounted on/dev/sda1 15G 2.8G 11G 21% /udev 488M 4.0K 488M 1% /devtmpfs 199M 788K 198M 1% /runnone 5.0M 0 5.0M 0% /run/locknone 497M 200K 496M 1% /run/shmroot@ubuntu:~#
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表